import {Controller, Get, Post, UploadedFile, UseInterceptors} from '@nestjs/common';
import {AppService} from './app.service';
import {ApiImplicitFile} from "@nestjs/swagger/dist/decorators/api-implicit-file.decorator";
import {ApiOperation, ApiTags} from "@nestjs/swagger";
import {FileInterceptor} from "@nestjs/platform-express";
import {ConfigService} from "@nestjs/config";
import {EnvEnum} from "../../../libs/common/src/enum/envEnum";

@Controller()
@ApiTags("默认")
export class AppController {
  constructor(private readonly adminService: AppService, private readonly configService: ConfigService) {
  }

  @Get()
  getHello(): string {
    return this.adminService.getHello();
  }


  @ApiImplicitFile({
    name: '课程封面',
    description: '上传课程封面',
    required: true,
  })
  @ApiOperation({summary: '上传文件'})
  @Post('uploads')
  @UseInterceptors(FileInterceptor('file')) // file表示文件字段名
  async upload(@UploadedFile('file') file: Express.Multer.File) {
    const isOSS = this.configService.get<boolean>(EnvEnum.is_oss, {infer: true})
    const port = this.configService.get<number>(EnvEnum.admin_port, {infer: true})
    const url = isOSS === 'true' ?
        `https://heart-topfullstack.oss-cn-beijing.aliyuncs.com/${file.filename}` :
        `http://localhost:${Number(port)}/uploads/${file.filename}`
    return {
      url
    }
  }
}
